package com.chuangke.admin.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.admin.entity.SysDept;
import com.chuangke.admin.service.SysDeptService;
import com.chuangke.common.http.HttpResult;

/**
 * 机构控制器
 * @author chuangke
 * @date Oct 29, 2018
 */
@RestController
@RequestMapping("dept")
public class SysDeptController {

	@Autowired
	private SysDeptService sysDeptService;
	
	@PreAuthorize("hasAuthority('sys:dept:add') AND hasAuthority('sys:dept:edit')")
	@PostMapping(value="/save")
	public HttpResult save(@RequestBody SysDept record) {
		sysDeptService.save(record) ;
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('sys:dept:delete')")
	@PostMapping(value="/delete")
	public HttpResult delete(@RequestBody List<SysDept> records) {
		sysDeptService.delete(records);
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value="/findTree")
	public HttpResult findTreeGet() {
		return HttpResult.ok(sysDeptService.findTree());
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value="/findbytype")
	public HttpResult findByType(@RequestBody Map<String,Object> params) {
		String deptType = (String) params.get("deptType") ;
		return HttpResult.ok(sysDeptService.findByDeptType(deptType));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value="/tree")
	public HttpResult findTree(@RequestBody Map<String,Object> params) {
		String fuzzy = (String) params.get("fuzzy") ;
		return HttpResult.ok(sysDeptService.findTree(fuzzy));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value="/findlastlevel")
	public HttpResult findLastLevelDept() {
		return HttpResult.ok(sysDeptService.findLastLevel());
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value="/postlastlevel")
	public HttpResult postLastLevelDept(@RequestBody Map<String,String> params) {
		List<SysDept> deptList = sysDeptService.findLastLevel() ;
		String type = params.get("type") ;
		deptList = deptList.stream().filter(dept->dept.getDeptType().equals(type)).collect(Collectors.toList()) ;
		return HttpResult.ok(deptList);
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value="/findlevel/{level}")
	public HttpResult findLevelDept(@PathVariable("level") int level) {
		return HttpResult.ok(sysDeptService.findByLevel(level));
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value="/level")
	public HttpResult level() {
		sysDeptService.level() ;
		return HttpResult.ok();
	}

}
